package com.jeremyfeinstein.okhttputil.cookie;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

public final class SimpleCookieJar implements CookieJar {
	private PersistentCookieStore cookieStore;

	public SimpleCookieJar(PersistentCookieStore cookieStore) {
		super();
		this.cookieStore = cookieStore;
	}

	@Override
	public synchronized void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
		if (cookies != null && cookies.size() > 0) {
			for (Cookie item : cookies) {
				cookieStore.add(url, item);
			}
		}
	}

	@Override
	public synchronized List<Cookie> loadForRequest(HttpUrl url) {
		List<Cookie> result = cookieStore.get(url);
		return result;
	}
}
